package com.yanqu.road.server.manager.activity.snowgarden;

import com.yanqu.road.entity.activity.CrossActivityMallDayBuyData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMallBuyDayInfo {
    private int activityId;
    private int activityDay;
    //key mallId
    private Map<Integer, CrossActivityMallDayBuyData> mallBuyDataMap = new ConcurrentHashMap<>();

    public CrossMallBuyDayInfo(int activityId, int activityDay) {
        this.activityId = activityId;
        this.activityDay = activityDay;
    }

    public int getActivityId() {
        return activityId;
    }

    public int getActivityDay() {
        return activityDay;
    }

    public Map<Integer, CrossActivityMallDayBuyData> getMallBuyDataMap() {
        return mallBuyDataMap;
    }

    public void addMallDayBuyData(CrossActivityMallDayBuyData dayBuyData) {
        mallBuyDataMap.put(dayBuyData.getMallId(), dayBuyData);
    }

    public CrossActivityMallDayBuyData getMallBuyData(int mallId) {
        return mallBuyDataMap.get(mallId);
    }

    public void updateServerMallBuy(long serverId, int mallId, long count) {
        CrossActivityMallDayBuyData dayBuyData = mallBuyDataMap.get(mallId);
        if (dayBuyData == null) {
            synchronized (mallBuyDataMap){
                dayBuyData = mallBuyDataMap.get(mallId);
                if (dayBuyData == null) {
                    dayBuyData = new CrossActivityMallDayBuyData(activityId, mallId, activityDay);
                    mallBuyDataMap.put(mallId, dayBuyData);
                }
            }
        }
        synchronized (dayBuyData){
            dayBuyData.setServerMallBuy(serverId, count);
        }
    }

    public List<CrossActivityMallDayBuyData> getBuyDataList() {
        List<CrossActivityMallDayBuyData> list = new ArrayList<>();
        synchronized (mallBuyDataMap) {
            list.addAll(mallBuyDataMap.values());
        }
        return list;
    }

    public CrossActivityMallDayBuyData initDayBuyData(int mallId) {
        CrossActivityMallDayBuyData dayBuyData = mallBuyDataMap.get(mallId);
        if (dayBuyData == null) {
            synchronized (mallBuyDataMap){
                dayBuyData = mallBuyDataMap.get(mallId);
                if (dayBuyData == null) {
                    dayBuyData = new CrossActivityMallDayBuyData(activityId, mallId, activityDay);
                    mallBuyDataMap.put(mallId, dayBuyData);
                }
            }
        }
        return dayBuyData;
    }
}
